var layerName=[];
var confirmServer;
function socketInit(socketServer){
    confirmServer = socketServer;
    var wsURL = "ws:"+socketServer+"/websocket";
    var websocket = null;
    if('WebSocket' in window){
        websocket = new WebSocket(wsURL);
    }else {
        alert("Not support websocket")
    }

    websocket.onerror = function(){
        alert("Socket link failed");
    };

    window.onbeforeunload = function(){
        websocket.close();
    }

    websocket.onmessage = function(event){
        var jsonObj = JSON.parse(event.data).data;

        if(jsonObj[0].log_out !== undefined){
            location.reload();
        }

        layer.closeAll();
        alertIdArr = [];
        for(var i in jsonObj){
            var obj = {
                id:jsonObj[i].alarm_origin_id,
                location:jsonObj[i].geo,
                name:jsonObj[i].name,
                dev_status:jsonObj[i].dev_status,
                picture:jsonObj[i].geo_level_3_id,
                pos_x:jsonObj[i].pos_x,
                pos_y:jsonObj[i].pos_y,
                times:jsonObj[i].alarm_time,
                sensor_name:jsonObj[i].sensor_name,
                cancel_id:jsonObj[i].cancel_id,
            }
            if(!obj.cancel_id){
                var stdLayerName = {name:"index"+obj.id};
                alertIdArr.push(obj.id);
                stdLayerName.num =  layer.open({
                    type:1,
                    area: ['1000px', '100%'],
                    title: obj.times+' '+'在'+obj.location+"  "+obj.name+' 发生 '+obj.dev_status,
                    shade:false,
                    maxmin:true,
                    maxHeight:'1000px',
                    scrollbar: false,
                    skin:'layui-layer-hong',
                    content: '<div class="row" style="width: 100%;height: 100%;margin-left: 0px">'
                    +'<audio src="assets/报警铃声.mp3" autoplay="autoplay" loop="loop" style="display: none"></audio>'
                    +'       <div id="iPicture_layer'+obj.id+'" data-interaction="hover" style="width: 100%;height: 100%">'
                    +'           <div class="ip_slide" id="map_div_layer'+obj.id+'" style="width: 100%;height: 100%">'
                    +'           </div>'
                    +'       </div>'
                    +'</div>',
                    btn:['确认','忽略'],
                    btn1:function (index, layero) {
                        confirmAlert(alertIdArr.pop());
                        layer.close(index);
                        location.reload();
                    },
                    btn2:function (index, layero) {
                        location.reload();
                        layer.close(index);
                        location.reload();
                    }
                    ,zIndex: layer.zIndex
                    ,success: function(layero){
                        layer.setTop(layero);
                        var imgs = "";
                        imgs = '<img id="show_map_layer" style="width: 100%;height: 100%" class="ip_tooltipImg"  src="http:'+socketServer+'/xkGeoLocationMap/mapDownload?id='+obj.picture+'" />';
                        $('#map_div_layer'+obj.id).append(imgs);
                        var x = (parseFloat(obj.pos_x) * 100);
                        x = x>0?x+'%':0;
                        var y = (parseFloat(obj.pos_y) * 100);
                        y = y>0?y+'%':0;
                        var divs ="";
                        divs ='<div class="ip_tooltip ip_img32" style="top:calc('+y+' - 9px); left: calc('+x+' - 7px);" data-tooltipbg="bgwhite" data-round="roundBgR" data-animationtype="btt-slide">' +
                            '</div>';
                        $('#map_div_layer'+obj.id).append(divs);
                        $( "#iPicture_layer"+obj.id ).iPicture_base();
                    }
                })
                layerName.push(stdLayerName);
            }else {
                for(var i in layerName){
                    if(layerName[i].name == "index"+obj.cancel_id){
                        layer.close(layerName[i].num);
                        layerName.splice(i,1);
                    }
                }
            }
        }
    };
}


function confirmAlert(id) {
    $.ajax({
        url:'http:'+confirmServer+ '/alarm/confirm?alarm_id='+ id,
        success:function (data) {

        }
    });
}

